package com.learn;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/01/21
 * @description 事务全注解开发
 */
@Configuration
@ComponentScan("com.learn")
@EnableTransactionManagement
public class SpringConfig {
    @Bean("dataSource")
    public DruidDataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test_new");
        dataSource.setUsername("root");
        dataSource.setPassword("9UpqQ6cga");
        return dataSource;
    }
    @Bean("jdbcTemplate")
    public JdbcTemplate getJdbcTemplate(DataSource ds) {
        return new JdbcTemplate(ds);
    }
    @Bean("txManager")
    public DataSourceTransactionManager getTxManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
